package io.github.heollhai.common.config.reids;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author: lmf
 * @Create: 2024/7/13 16:22
 * Description:
 */
@Configuration
@Data
public class RedissonConfig {
    @Value("${spring.data.redis.host}")
    private String redisHost;
    @Value("${spring.data.redis.port}")
    private Integer redisPort;
    @Value("${spring.data.redis.password}")
    private String redisPassword;


    //  redis 分布式锁 相关配置用到了再说
//    @Bean
//    public RedissonClient redissonClient() throws IOException {
//        Config config = new Config();
//        config.useSingleServer()
//                .setAddress("redis://192.168.8.21:5000")
//                ; // 设置密码
//        return Redisson.create(config);
//    }

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        // 初始化RedisTemplate实例，用于操作Redis数据库
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        // 设置RedisTemplate的连接工厂，该工厂负责创建与Redis服务器的连接
        redisTemplate.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(serializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;

    }
}
